import { constantRouterMap, defaultRouters } from '@/router'

const permission = {
    state: {
      routers: constantRouterMap,
      addRouters: []
    },
    mutations: {
      SET_ROUTERS: (state, routers) => {
        state.addRouters = routers
        state.routers = constantRouterMap.concat(routers)
      }
    },
    actions: {
      GenerateRoutes({ commit }, data) {
        return new Promise(resolve => {
            var arr = []
            for(let item of data){
                arr.push(item.menuUrl)
                if(item.children !== null){
                    for(let secItem of item.children) {
                        arr.push(secItem.menuUrl)
                    }
                }
            }
            var routersLsit = []
            defaultRouters.map((item,index)=>{
                for(let secItem of arr){
                    if(secItem == item.name){
                        routersLsit.push(item) //先把最外层的放进去
                    }
                }
            })
            routersLsit.map((item,index)=>{
                if(item.name == "systemMange" || item.name == "baseInfo"){
                    if(item.children) {
                        for(var i = 0; i<item.children.length; i++){
                            if(arr.indexOf(item.children[i].name) === -1){
                                item.children.splice(i,1)
                                i--
                             }
                        }
                    }
                }
            })
          commit('SET_ROUTERS', routersLsit)
          resolve()
        })
      }
    }
  }
  
  export default permission
